//
//  AddContactsViewController.m
//  Only Love
//
//  Created by 郭健 on 16/5/11.
//  Copyright © 2016年 海城. All rights reserved.
//

#import "AddContactsViewController.h"
#import "CurrentUserInfo.h"

#define kColor [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:0.4]

@interface AddContactsViewController ()
@property (nonatomic,retain)UITextField *userNameTextField;     //输入用户名
@end

@implementation AddContactsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.userNameTextField];      //添加UITextField视图
    self.view.backgroundColor =kColor;             //UIView 背景颜色
    self.navigationItem.title = @"添加好友";        //标题
    
    /** 添加按钮*/
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(kWidth/2-20,kHeight/3,40,40);
    [button setTitle:@"完成" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    /** 添加后退按钮*/
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"< 通讯录" style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
}

//懒加载
-(UITextField *)userNameTextField{
    
    if (!_userNameTextField) {
        _userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(kWidth/2-100, kHeight/2-150, 200, 30)];
        _userNameTextField.placeholder = @"请输入用户名" ;
        _userNameTextField.textAlignment = NSTextAlignmentCenter;
        _userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _userNameTextField;
}
    //按钮的回调方法
-(void)buttonAction{
    EMError *error = nil;
    BOOL isSuccess = [[EaseMob sharedInstance].chatManager addBuddy:self.userNameTextField.text message:[NSString stringWithFormat:@"%@想要添加您为好友",[CurrentUserInfo sharedCurrentUserInfo].userName] error:&error];
    if (isSuccess) {
//        NSLog(@"发送邀请失败 -- %d",error.code);
    }else{
        NSLog(@"发送邀请成功");
        //发送邀请成功之后，返回联系人列表
        [self.navigationController popViewControllerAnimated:YES];
        //        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }


}

/** 后退按钮的回调方法*/
-(void)backAction:(UIBarButtonItem *)sender{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
